package ru.urfc.portal.model.radio;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import ru.urfc.portal.model.SpectrObject;
import ru.urfc.portal.model.catalogs.DeviceState;
import ru.urfc.portal.ui.client.Constants;

@Table(name = "portal_devicestatechanges")
@Entity
@XmlRootElement(name = Constants.DEVICE_STATE_CHANGE)
public class DeviceStateChange extends SpectrObject implements Comparable<DeviceStateChange> {
	
	private DeviceWithExtra device; 
	@Temporal(TemporalType.DATE)
	private Date changeDate;
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "deviceState_id")
	private DeviceState state;

	public DeviceStateChange() {
	}
	
	public DeviceStateChange(DeviceWithExtra device, Date changeDate, DeviceState state) {
		this.device = device; 
		this.changeDate = changeDate;
		this.state = state;
	}
	
	public DeviceStateChange(Long id, Date changeDate, DeviceState state) {
		super(id);
		this.changeDate = changeDate;
		this.state = state;
	}

	@XmlTransient
	public DeviceWithExtra getDevice() {
		return device;
	}
	
	@XmlElement
	public Date getChangeDate() {
		return changeDate;
	}
	
	@XmlElement
	public DeviceState getState() {
		return state;
	}

	@Override
	public int compareTo(DeviceStateChange o) {
		int datesCompare = getChangeDate().compareTo(o.getChangeDate());
		return datesCompare != 0? datesCompare: getId().compareTo(o.getId());
	}

}
